package com.iemes.controller.mould;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.ITransportFormMap;
import com.iemes.entity.LoadOrderFormMap;
import com.iemes.entity.MouldCheckStandardFormMap;
import com.iemes.entity.MouldUnpassHandleFormMap;
import com.iemes.entity.PAfterCheckFormMap;
import com.iemes.entity.PBeforeCheckFormMap;
import com.iemes.entity.PUnloadcheckFromMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.mould.ITransportMapper;
import com.iemes.mapper.mould.LoadOrderMapper;
import com.iemes.mapper.mould.MouldCheckStandardMapper;
import com.iemes.mapper.mould.MouldUnpassHandleMapper;
import com.iemes.mapper.mould.PAfterCheckMapper;
import com.iemes.mapper.mould.PUnloadcheckMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@Controller
@RequestMapping("/pAfterCheck/")
public class PAfterCheckController extends BaseController {

	@Inject
	private MouldUnpassHandleMapper mouldUnpassHandleMapper;
	
	@Inject
	private PAfterCheckMapper pAfterCheckMapper;
	
	@Inject
	private PUnloadcheckMapper pUnloadcheckMapper;
	
	@Inject
	private MouldCheckStandardMapper mouldCheckStandardMapper;
	
	@Inject
	private LoadOrderMapper loadOrderMapper;
	
	@Inject
	private ITransportMapper iTransportMapper;
	
	@RequestMapping("pAfterCheck_list")
	public String listUI(Model model) throws Exception {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/mould/pAfterCheck/pAfterCheck_list";
	}
	
	@ResponseBody
	@RequestMapping("pAfterCheck_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		PAfterCheckFormMap pAfterCheckFormMap=getFormMap(PAfterCheckFormMap.class);
		List<Map<String, Object>> after = pAfterCheckMapper.getAll(pAfterCheckFormMap);
		LuogicPaging(pageNow, pageSize, after);
		return pageView;
	}
	
	@RequestMapping("pAfterCheck_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			//根据id查询备模后模具质量表
			Map<String, Object> after = pAfterCheckMapper.getById(id);
			model.addAttribute("after",after);
			//获取全部用户
			UserFormMap userFormMap=getFormMap(UserFormMap.class);
			List<UserFormMap> users=baseMapper.findByNames(userFormMap);
			model.addAttribute("users",users);
			//获得相同checkId的检查标准
			MouldUnpassHandleFormMap mouldUnpassHandleFormMap=getFormMap(MouldUnpassHandleFormMap.class);
			mouldUnpassHandleFormMap.put("checkId", after.get("id"));
			List<MouldUnpassHandleFormMap> handle=mouldUnpassHandleMapper.getAfterHandle(mouldUnpassHandleFormMap);
			model.addAttribute("handle",handle);
		}
		return Common.BACKGROUND_PATH + "/mould/pAfterCheck/pAfterCheck_edit";
	}
	
	@ResponseBody
	@RequestMapping(value="pAfterCheck_editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="备模后模具质量检查",methods="备模后模具质量检查表-修改备模后模具质量检查表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws Exception {
		PAfterCheckFormMap pAfterCheckFormMap=getFormMap(PAfterCheckFormMap.class);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date date=new Date();
		pAfterCheckFormMap.put("checkDate", sdf.format(date));
		// 获取request
		request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		pAfterCheckFormMap.put("fillPeopleId", userFormMap.getInt("id"));
		pAfterCheckFormMap.put("status", 1);
		baseMapper.editEntity(pAfterCheckFormMap);
		
		//添加卸模前模具质量检查表（以后改为收到成型车间机台够产前30分钟通知后才添加）
		String machineId=pAfterCheckFormMap.get("id").toString();
		PUnloadcheckFromMap unload=pUnloadcheckMapper.getByMachineId(machineId);
		if(unload==null || unload.size()<=0){
			PUnloadcheckFromMap pUnloadcheckFromMap=new PUnloadcheckFromMap();
			pUnloadcheckFromMap.put("machineId", pAfterCheckFormMap.get("id"));
			pUnloadcheckFromMap.put("checkPeopleId", 11);
			baseMapper.addEntity(pUnloadcheckFromMap);
			//循环添加卸模前模具质量检查表检查标准
			MouldCheckStandardFormMap mouldCheckStandardFormMap = getFormMap(MouldCheckStandardFormMap.class);
			mouldCheckStandardFormMap.put("status", 1);
			List<Map<String, Object>> getAllItemBy3=mouldCheckStandardMapper.getAllItemBy3(mouldCheckStandardFormMap);
			for(Map<String, Object> map:getAllItemBy3){
				MouldUnpassHandleFormMap mouldUnpassHandleFormMap=getFormMap(MouldUnpassHandleFormMap.class);
				mouldUnpassHandleFormMap.put("standardId",map.get("id"));
				mouldUnpassHandleFormMap.put("checkId", pUnloadcheckFromMap.get("id"));
				mouldUnpassHandleFormMap.put("repairPeopleId",11);
				mouldUnpassHandleFormMap.put("supervisorId",11);
				baseMapper.addEntity(mouldUnpassHandleFormMap);
			}
		}
		//添加装模指令表
		String pAfterCheckId=pAfterCheckFormMap.get("id").toString();
		LoadOrderFormMap load=loadOrderMapper.getBypAfterCheckId(pAfterCheckId);
		if(load==null || load.size()<=0){
			LoadOrderFormMap loadOrderFormMap=new LoadOrderFormMap();
			loadOrderFormMap.put("pAfterCheckId", pAfterCheckFormMap.get("id"));
			loadOrderFormMap.put("sendPeopleId", 11);
			baseMapper.addEntity(loadOrderFormMap);
		}
		//添加装模运输表（以后改为收到机台够产信息后才添加）
		machineId=pAfterCheckFormMap.get("id").toString();
		ITransportFormMap iTransport=iTransportMapper.getByMachineId(machineId);
		if(iTransport==null || iTransport.size()<=0){
			ITransportFormMap iTransportFormMap=new ITransportFormMap();
			iTransportFormMap.put("machineId", pAfterCheckFormMap.get("id"));
			iTransportFormMap.put("beginTimePeopleId", 11);
			iTransportFormMap.put("endTimePeopleId", 11);
			iTransportFormMap.put("confirmTimePeopleId", 11);
			baseMapper.addEntity(iTransportFormMap);
		}
		//先删除
		baseMapper.deleteByAttribute("checkId","'"+Integer.parseInt(pAfterCheckFormMap.getStr("id"))+"'", MouldUnpassHandleFormMap.class);
		//再添加
		int checkId=Integer.parseInt(request.getParameter("pAfterCheckFormMap.id"));
		String[] standardId=request.getParameterValues("standardId_1");
		String[] checkResult=request.getParameterValues("checkResult_1");
		String[] handle=request.getParameterValues("handle_1");
		String[] handleTime=request.getParameterValues("handleTime_1");
		String[] repairPeopleId=request.getParameterValues("repairPeopleId_1");
		String[] repairResult=request.getParameterValues("repairResult_1");
		String[] remark=request.getParameterValues("remark_1");
		String[] finishRepair=request.getParameterValues("finishRepair_1");
		String[] finishTime=request.getParameterValues("finishTime_1");
		if(standardId!=null){
			for(int i=0;i<standardId.length;i++){
				MouldUnpassHandleFormMap mouldUnpassHandleFormMap=new MouldUnpassHandleFormMap();
				mouldUnpassHandleFormMap.put("checkId", checkId);
				mouldUnpassHandleFormMap.put("standardId", standardId[i]);
				mouldUnpassHandleFormMap.put("checkResult", checkResult[i]);
				mouldUnpassHandleFormMap.put("handle",handle[i]);
				mouldUnpassHandleFormMap.put("handleTime", handleTime[i]);
				mouldUnpassHandleFormMap.put("repairPeopleId", Integer.parseInt(repairPeopleId[i]));
				mouldUnpassHandleFormMap.put("repairResult",repairResult[i]);
				mouldUnpassHandleFormMap.put("remark",remark[i]);
				mouldUnpassHandleFormMap.put("finishRepair", Integer.parseInt(finishRepair[i]));
				mouldUnpassHandleFormMap.put("finishTime",finishTime[i]);
				mouldUnpassHandleFormMap.put("supervisorId", userFormMap.getInt("id"));
				baseMapper.addEntity(mouldUnpassHandleFormMap);
			}
		}
		return "success";
	}
}
